﻿Imports System.IO
Imports System.Runtime.Serialization

Imports IntoNails.Models

<Serializable()> _
Public Class AuthorityRepository
    Inherits AbstractFileRepository(Of Authority)
    Implements IAuthorityRepository, ISerializable

#Region "Constructors"

    Public Sub New(ByVal dataFile As String)
        Me.DataFile = dataFile

        'If IO.File.Exists(dataFile) Then
        '    Me.InnerDataList = Me.Load()
        'End If

        Me.Load()

        'If Me.InnerDataList.Count = 0 Then
        '    Dim admin As Authority = Authority.CreateAuthority(1, "Admin", Date.Today, True)
        '    Me.InnerDataList.Add(admin)
        '    Me.Save()
        'End If
    End Sub

    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        Me.InnerDataList = DirectCast(info.GetValue("AuthorityList", GetType(List(Of Authority))), List(Of Authority))
    End Sub

    Public Sub GetObjectData(ByVal info As SerializationInfo, ByVal ctxt As StreamingContext) _
        Implements ISerializable.GetObjectData
        info.AddValue("AuthorityList", Me.InnerDataList)
    End Sub

#End Region

#Region "Properties"

#End Region

#Region "RepositoryBase Overriden"

    Public Overrides Sub Load()
        If IO.File.Exists(Me.DataFile) Then
            Using readStream As New FileStream(Me.DataFile, FileMode.Open, FileAccess.Read)
                Me.InnerDataList = readStream.Deserialize(Of List(Of Authority))()
            End Using
        Else
            Me.InnerDataList = New List(Of Authority)()
        End If
    End Sub

    Public Overrides Sub Save()
        Using writeStream As New FileStream(Me.DataFile, FileMode.OpenOrCreate, FileAccess.Write)
            Me.InnerDataList.Serialize(writeStream)
        End Using
    End Sub

#End Region

#Region "IAuthorityRepository Members"

    Public Function GetAuthority(ByVal name As String) As Authority _
        Implements IAuthorityRepository.GetAuthority

        Return Me.InnerDataList.Where(Function(a) a.Name = name).FirstOrDefault()

    End Function

    Public Function GetValidAuthorityCollection() As List(Of Authority) _
        Implements IAuthorityRepository.GetValidAuthorityCollection

        Return Me.InnerDataList.Where(Function(a) a.IsValid = True).ToList()

    End Function

    Public Function Add(ByVal data As Authority) As Boolean _
        Implements IDataRepository(Of Authority).Add

        If Not Me.InnerDataList.Contains(data) Then
            Me.InnerDataList.Add(data)
            Return True
        End If

        Return False
    End Function

    Public Function GetItem(ByVal uniqueCode As Integer) As Authority _
        Implements IDataRepository(Of Authority).GetItem

        Return Me.InnerDataList.Where(Function(a) a.UniqueCode = uniqueCode).FirstOrDefault()

    End Function

    Public Function GetRawCollection() As List(Of Authority) _
        Implements IDataRepository(Of Authority).GetRawCollection

        Return Me.InnerDataList
    End Function

    Public Function Remove(ByVal uniqueCode As Integer) As Boolean _
        Implements IDataRepository(Of Authority).Remove

        Dim removed As Authority = Me.GetItem(uniqueCode)
        If removed IsNot Nothing Then
            removed.IsValid = False
            removed.DateChanged = Date.Today
            Return True
        End If

        Return False
    End Function

    Public Function Update(ByVal uniqueCode As Integer, ByVal newData As Authority) _
        As Boolean _
        Implements IDataRepository(Of Authority).Update

        Dim updated As Authority = Me.GetItem(uniqueCode)
        If updated Is Nothing Then
            Return False
        End If

        If newData Is Nothing Or newData.UniqueCode <> uniqueCode Then
            Return False
        End If

        With updated
            updated.Name = newData.Name
            updated.IsValid = newData.IsValid
            updated.DateChanged = Date.Today
        End With

        Return True
    End Function

#End Region

End Class
